package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitivePageReqDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    /**
     * 分页查询敏感词
     * @return
     */
    @Override
    public ResponseResult SensitiveList(SensitivePageReqDto dto) {
        //1.检查参数
        //分页查询
        dto.checkParam();
        //2.分页查询
        LambdaQueryWrapper<WmSensitive> queryWrapper=new LambdaQueryWrapper<>();
        IPage page = new Page(dto.getPage(), dto.getSize());
        if (StringUtils.isNotBlank(dto.getName())||dto.getName()!=null){
            queryWrapper.like(WmSensitive::getSensitives,dto.getName());
        }
        //按照时间倒序查找
        queryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        //查找集合封装到page中
        page=page(page,queryWrapper);
        //返回结果
        ResponseResult responseResult=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 通过id删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult SensitiveDel(Integer id) {
        //1.检查参数
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive wmSensitive = getById(id);
        if (wmSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //2.删除敏感词
        removeById(id);
        //3.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 新建敏感词--保存敏感词
     * @param wmSensitive
     * @return
     */
    public ResponseResult SensitiveSave(WmSensitive wmSensitive){
        //1.检查参数
        if (wmSensitive.getSensitives()==null||StringUtils.isBlank(wmSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.如果是已存在的敏感词则不可以保存
        LambdaQueryWrapper<WmSensitive> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(WmSensitive::getSensitives,wmSensitive.getSensitives());
        WmSensitive sensitive = getOne(queryWrapper);
        if (sensitive!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     * @param wmSensitive
     * @return
     */
    public ResponseResult SensitiveUpdate(WmSensitive wmSensitive){
        //1.检查参数
        if (wmSensitive.getSensitives()==null||StringUtils.isBlank(wmSensitive.getSensitives())||wmSensitive.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询参数是否存在
        WmSensitive sensitive = getById(wmSensitive.getId());
        if (sensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.查询修改内容是否为已存在改敏感词
        LambdaQueryWrapper<WmSensitive> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(WmSensitive::getSensitives,wmSensitive.getSensitives());
        if (getOne(queryWrapper)!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"修改的后敏感词已存在在表中");
        }
        sensitive.setSensitives(wmSensitive.getSensitives());
        sensitive.setCreatedTime(new Date());
        updateById(sensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
